package de.bazinga.rotw.weapon.grenade;

import com.jme.bounding.BoundingBox;
import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.jme.scene.shape.Sphere;

import de.bazinga.rotw.person.Person;
import de.bazinga.rotw.texture.TextureCache;

public class GrenadeObject extends Sphere {
	private static final long serialVersionUID = 1L;

	private Grenade granadeWeapon = null;
	
	private Node rootNode = null;
	private Vector3f startPosition = null;
	
	private GrenadeController controller = null;
	
	private boolean active = false;
	
	private Person person = null;
	
	public GrenadeObject(Grenade granadeWeapon, Person person, Node rootNode, Vector3f location, Vector3f direction) {
		super("granade",new Vector3f(0,0,0),10,10,0.05f);
		
		this.person = person;
		this.setLocalTranslation(location);
		this.granadeWeapon = granadeWeapon;
		this.rootNode = rootNode;
		
		this.startPosition = location.clone();
		
		this.active = true;

		this.setModelBound(new BoundingBox());
		this.updateModelBound();
		
		this.setRenderState(TextureCache.getInstance().getTexture("texture/camouflage.jpg"));
		this.updateRenderState();
		
		
		rootNode.attachChild(this);
		rootNode.updateRenderState();
		
		
		// Add Controller
		controller = new GrenadeController(this,direction);
		this.addController(controller);
	}
	
	
	
	public void reset(Grenade granadeWeapon, Person person, Vector3f location, Vector3f direction) {
		this.setLocalTranslation(location);
		this.person = person;
		this.granadeWeapon = granadeWeapon;
		this.startPosition = location.clone();
		
		if(controller != null) {
			controller.setDirection(direction);
		}
	}
	
	
	public void setActive(boolean active) {
		if(active) {
			// Set Active
			controller.setActive(true);
			this.setCullHint(CullHint.Never);
		}else{
			// Set Inactive
			controller.setActive(false);
			this.setLocalTranslation(new Vector3f(9999,9999,9999));
			this.setCullHint(CullHint.Always);
			GrenadeList.getInstance().addUnusedGranade(this);
			GrenadeList.getInstance().removeGranade(this); // Remove Bullet
		}
		
		this.active = active;
	}
	
	public boolean isActive() {
		return active;
	}

	
	
	public void handleCollision() {
		granadeWeapon.handleCollision(person, rootNode, getLocalTranslation());
	}


	public Vector3f getStartPosition() {
		return startPosition;
	}



	public void setStartPosition(Vector3f startPosition) {
		this.startPosition = startPosition;
	}
}
